﻿function createAlertMessage(date, gender) {
	var now = new Date();
	var d = new Date(now.getFullYear(), date.getMonth(), date.getDate());
	if(now - d < 0){
		return "";
	}
	else if(now - d == 0){
		return "<span class='alertMsg'>(Today is "+ gender +" birthday. Send </span>"
	}
}
function init(){
    // create the opensocial.DataRequest object.
    var request = opensocial.newDataRequest();

    // create a list of Person fields to fetch,
    // in this case, include all supported fields.
    var fields = [  
					opensocial.Person.Field.AGE,
					opensocial.Person.Field.BODY_TYPE,
					opensocial.Person.Field.BOOKS,
					opensocial.Person.Field.CHILDREN,
					opensocial.Person.Field.CURRENT_LOCATION,
					opensocial.Person.Field.DATE_OF_BIRTH,
					opensocial.Person.Field.NAME,
					opensocial.Person.Field.NICKNAME,
					opensocial.Person.Field.PROFILE_SONG,
					opensocial.Person.Field.PROFILE_URL,
					opensocial.Person.Field.RELATIONSHIP_STATUS,
					opensocial.Person.Field.RELIGION,
					opensocial.Person.Field.SEXUAL_ORIENTATION,
					opensocial.Person.Field.SMOKER,
					opensocial.Person.Field.STATUS,
					opensocial.Person.Field.THUMBNAIL_URL,
					opensocial.Person.Field.TV_SHOWS,
					opensocial.Person.Field.GENDER,
					opensocial.Person.Field.URLS];

	var params = {};
	params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = fields;
    params[opensocial.DataRequest.DataRequestFields.ESCAPE_TYPE] = opensocial.EscapeType.HTML_ESCAPE;

	var idspec = opensocial.newIdSpec({ "userId" : "OWNER", "groupId" : "FRIENDS" });
	var req = opensocial.newDataRequest();
	req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.OWNER, params), "get_owner");
	req.add(req.newFetchPeopleRequest(idspec, params), "get_friends");
    req.send(got_init);
}

// the callback function for the initial data requests, 'response' is the opensocial.DataResponse object.
function got_init(response){

	owner = response.get('get_owner').getData();
	friends = response.get('get_friends').getData(); 
	
	var content = document.getElementById("content");
	friends.each(function(person) {
		content.innerHTML += createPerson(person);
	});
	
        // hide the spinner and show the content now that the DOM has been populated.
    document.getElementById("spinner").style.display = "none";
    document.getElementById("content").style.display = "block";
	
    // make the iframe fit the new content.
    gadgets.window.adjustHeight();
}

// parses the fields of the current owner and outputs it to the page
function createPerson(person){
	var htmlCode = 
	"<div> \
		<div id='headline' class='divider'><strong>{0}</strong></div> \
        <div class='float'> \
            <center class='imageLinks'> \
                <a href=''><img src='{1}'/></a> \
           </center> \
        </div> \
        <div id='details'>{2}</div> \
        <div class='clear'></div> \
    </div>";
	
	
	for(var p in person.fields_){
			alert(p+":"+person.fields_[p]);
	}
		
	var dob = person.getField(opensocial.Person.Field.DATE_OF_BIRTH);
	var gender = person.getField(opensocial.Person.Field.GENDER);
	
	alert(gender.getDisplayValue());
    var details = StringHandler.format("{0}{1} years old <br/>{2}<a href='#'>Send gift</a> | <a href='#'>Send message</a>"
					,gender ? gender.getDisplayValue() + "<br/>" : ""
					,person.getField(opensocial.Person.Field.AGE)
					,dob ? dob.format("mmmm d, yyyy") + "<br/>" : ""
					);
	htmlCode = StringHandler.format(htmlCode
					,person.getDisplayName()
					,person.getField(opensocial.Person.Field.THUMBNAIL_URL)
					,details);
		
	return htmlCode;
}

var owner;
var friends;

